<?php

class ml_model_guang_dbGoodsBase extends Lib_datamodel_db 
{
    const ORDERBY_TIME = '`ctime` desc';
    const ORDERBY_HOT = '`sort` desc , `etime` desc, `hot_rank` desc';
    
    public function __construct()
    {
        $db_config = ml_factory::load_standard_conf('dbAdmin');
        parent::__construct('admin' , $db_config['admin']);
        
    }
    
    public function get_total()
    {
        if(!$this->init_db())
            return false;
        
        return $this->fetch_count();
    }
    
    public function list_all($start , $pagesize = 40 , $orderby = self::ORDERBY_TIME)
    {
        if(!$this->init_db())
            return false;
    
        $sql = "select * from ".$this->table." order by $orderby limit ".$start.",".$pagesize;
        return $this->fetch($sql);
    }
    
    public function add_goods($rid , $data)
    {
        if(!$this->init_db())
            return false;

        $data['content_rid'] = $rid;
        return $this->insert($data);
    }
    
    public function del_by_rid($rid)
    {
        if(!$this->init_db())
            return false;
            
        $sql = 'delete from '.$this->table.' where `content_rid`="'.$this->escape($rid).'" limit 1';
        echo $sql;
        return $this->query($sql);
    }
    
    public function update_score($rid , $like_cnt , $cmt_cnt , $pv , $hot_rank)
    {
        if(!$this->init_db())
            return false;
        $aUpdate = array(
            'like_cnt' => $like_cnt,
            'cmt_cnt' => $cmt_cnt,
            'pv' => $pv,
            'hot_rank' => $hot_rank,
        );
        $where = '`content_rid`="'.$rid.'"';
        return $this->update($aUpdate , $where);
    }
    
}
